package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.KongshiTimesDao;
import com.int_tech.MessageCodeAll.dao.XiecheTimesDao;
import com.int_tech.MessageCodeAll.dao.ZhongshiTimesDao;
import com.int_tech.MessageCodeAll.dao.ZhuangcheTimesDao;
import com.int_tech.MessageCodeAll.entity.KongshiTimes;
import com.int_tech.MessageCodeAll.entity.XiecheTimes;
import com.int_tech.MessageCodeAll.entity.ZhongshiTimes;
import com.int_tech.MessageCodeAll.entity.ZhuangcheTimes;
import com.int_tech.car.dao.CarInfoDataDao;
import com.int_tech.car.entity.CarInfoData;
import com.int_tech.mession.entity.Mession;
import com.int_tech.utils.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * Created by liubochao on 2018/6/30.
 */
@Component
public class CheckGongshiTimeTask {

    @Autowired
    private ZhuangcheTimesDao zhuangcheTimesDao;

    @Autowired
    private CarInfoDataDao carInfoDataDao;

    @Autowired
    private XiecheTimesDao xiecheTimesDao;

    @Autowired
    private ZhongshiTimesDao zhongshiTimesDao;

    @Autowired
    private KongshiTimesDao kongshiTimesDao;

    /**
     * 校验装车工时
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkZhuangcheGongshi() throws ParseException {
        //校验装车时间
        List<ZhuangcheTimes> zhuangcheTimesList = zhuangcheTimesDao.findByStatus(0);


        for(ZhuangcheTimes zhuangcheTimes : zhuangcheTimesList)
        {
            zhuangcheTimes.setStatus(1);
            zhuangcheTimesDao.save(zhuangcheTimes);

            try
            {
                String timeString = zhuangcheTimes.getDateString();
                String carNumber = zhuangcheTimes.getCarNumber();

                String unkey = timeString+"_"+carNumber;

                CarInfoData carInfoData = carInfoDataDao.findByUnKey(unkey);

                if(carInfoData == null) {
                    carInfoData = new CarInfoData();

                    carInfoData.setUnKey(unkey);
                    carInfoData.setTimeDate(unkey.split("_")[0]);
                    carInfoData.setCarNumber(carNumber);
                    carInfoData.setCreateTime(DateTool.string2Date(timeString + " 01:00:00", "yyyy-MM-dd HH:mm:ss"));

                    carInfoData.setOnCarTime(zhuangcheTimes.getOnCarTime());
                }
                else
                {
                    carInfoData.setOnCarTime(carInfoData.getOnCarTime() + zhuangcheTimes.getOnCarTime());
                }

                carInfoDataDao.save(carInfoData);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                zhuangcheTimes.setStatus(-1);
                zhuangcheTimesDao.save(zhuangcheTimes);
            }
        }
    }


    /**
     * 卸车工时计算
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkXiecheGongshi() throws ParseException {
        //校验卸车时间
        List<XiecheTimes> xiecheTimesList = xiecheTimesDao.findByStatus(0);


        for(XiecheTimes xiecheTimes : xiecheTimesList)
        {
            xiecheTimes.setStatus(1);
            xiecheTimesDao.save(xiecheTimes);

            try
            {
                String timeString = xiecheTimes.getDateString();
                String carNumber = xiecheTimes.getCarNumber();

                String unkey = timeString+"_"+carNumber;

                CarInfoData carInfoData = carInfoDataDao.findByUnKey(unkey);

                if(carInfoData == null) {
                    carInfoData = new CarInfoData();

                    carInfoData.setUnKey(unkey);
                    carInfoData.setTimeDate(unkey.split("_")[0]);
                    carInfoData.setCarNumber(carNumber);
                    carInfoData.setCreateTime(DateTool.string2Date(timeString + " 01:00:00", "yyyy-MM-dd HH:mm:ss"));

                    carInfoData.setOffCarTime(xiecheTimes.getOffCarTime());
                }
                else
                {
                    carInfoData.setOffCarTime(carInfoData.getOffCarTime() + xiecheTimes.getOffCarTime());
                }

                carInfoDataDao.save(carInfoData);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                xiecheTimes.setStatus(-1);
                xiecheTimesDao.save(xiecheTimes);
            }
        }
    }


    /**
     * 校验重驶工时
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkZhongshiGongshi() throws ParseException {
        //校验重驶时间
        List<ZhongshiTimes> zhongshiTimes = zhongshiTimesDao.findByStatus(0);


        for(ZhongshiTimes zhongshiTimes1 : zhongshiTimes)
        {
            zhongshiTimes1.setStatus(1);
            zhongshiTimesDao.save(zhongshiTimes1);

            try
            {
                String timeString = zhongshiTimes1.getDateString();
                String carNumber = zhongshiTimes1.getCarNumber();

                String unkey = timeString+"_"+carNumber;

                CarInfoData carInfoData = carInfoDataDao.findByUnKey(unkey);

                if(carInfoData == null) {
                    carInfoData = new CarInfoData();

                    carInfoData.setUnKey(unkey);
                    carInfoData.setTimeDate(unkey.split("_")[0]);
                    carInfoData.setCarNumber(carNumber);
                    carInfoData.setCreateTime(DateTool.string2Date(timeString + " 01:00:00", "yyyy-MM-dd HH:mm:ss"));

                    carInfoData.setHeavyTime(zhongshiTimes1.getZhongshiTime());
                }
                else
                {
                    carInfoData.setHeavyTime(carInfoData.getHeavyTime() + zhongshiTimes1.getZhongshiTime());
                }

                carInfoDataDao.save(carInfoData);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                zhongshiTimes1.setStatus(-1);
                zhongshiTimesDao.save(zhongshiTimes1);
                }
        }
    }


    /**
     * 校验空驶工时
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkKongshiGongshi() throws ParseException {
        //校验重驶时间
        List<KongshiTimes> kongshiTimesList = kongshiTimesDao.findByStatus(0);


        for(KongshiTimes kongshiTimes : kongshiTimesList)
        {
            kongshiTimes.setStatus(1);
            kongshiTimesDao.save(kongshiTimes);

            try
            {
                String timeString = kongshiTimes.getDateString();
                String carNumber = kongshiTimes.getCarNumber();

                String unkey = timeString+"_"+carNumber;

                CarInfoData carInfoData = carInfoDataDao.findByUnKey(unkey);

                if(carInfoData == null) {
                    carInfoData = new CarInfoData();

                    carInfoData.setUnKey(unkey);
                    carInfoData.setTimeDate(unkey.split("_")[0]);
                    carInfoData.setCarNumber(carNumber);
                    carInfoData.setCreateTime(DateTool.string2Date(timeString + " 01:00:00", "yyyy-MM-dd HH:mm:ss"));

                    carInfoData.setEmptyTime(kongshiTimes.getKongshiTime());
                }
                else
                {
                    carInfoData.setEmptyTime(carInfoData.getEmptyTime() + kongshiTimes.getKongshiTime());
                }

                carInfoDataDao.save(carInfoData);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                kongshiTimes.setStatus(-1);
                kongshiTimesDao.save(kongshiTimes);
            }
        }
    }


    @Scheduled(cron="0 0/10 * * * ?")
    public void kongxianGongshi() throws ParseException {

        List<CarInfoData> carInfoDataList = carInfoDataDao.findAll();

        for (CarInfoData carInfoData: carInfoDataList)
        {
            float kongxian = 24 - carInfoData.getHeavyTime() - carInfoData.getEmptyTime() - carInfoData.getOffCarTime() - carInfoData.getOnCarTime();
            System.out.print(kongxian);
            carInfoData.setLeisureTime(kongxian);

            carInfoDataDao.save(carInfoData);
        }

    }
}
